package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.common.util.JwtUtils;
import com.heima.model.media.dtos.WmUserDto;
import com.heima.model.media.dtos.WmUserLoginDto;
import com.heima.wemedia.entity.WmUser;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author heima
 * @since 2021-03-13
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {
    @Override
    public WmUserDto findByApUserId(Integer apUserId){
        //组装查询条件
        QueryWrapper<WmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WmUser::getApUserId,apUserId);
        //执行查询
        WmUser wmUser = getOne(queryWrapper);
        if(wmUser!=null){
            return BeanHelper.copyProperties(wmUser,WmUserDto.class);
        }
        return null;
    }

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public Map<String, Object> login(WmUserLoginDto dto) {
        //基于用户名查询用户
        QueryWrapper<WmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WmUser::getName,dto.getName());
        WmUser wmUser = getOne(queryWrapper);
        if(wmUser==null){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST,"用户名错误");
        }
        //比对密码是否正确
        boolean matches = passwordEncoder.matches(dto.getPassword(), wmUser.getPassword());
        if(!matches){
            throw new LeadException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //生成token
        String token = JwtUtils.generateTokenExpireInSeconds(wmUser.getId(), 3600);
        //组装返回结果
        Map<String,Object> map = new HashMap<>();
        wmUser.setPassword("");
        wmUser.setEmail("");
        map.put("token",token);
        map.put("user",wmUser);
        return map;
    }

    @Override
    public Integer saveWmUser(WmUserDto dto) {
        //基于App用户id查询自媒体用户
        WmUserDto wmUserDto = findByApUserId(dto.getApUserId());
        //如果存在，将id直接返回即可
        if(wmUserDto!=null){
            return wmUserDto.getId();
        }
        //如果不存在，创建自媒体用户
        WmUser wmUser = BeanHelper.copyProperties(dto, WmUser.class);
        wmUser.setCreatedTime(new Date());
        //执行保存
        boolean b = this.save(wmUser);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR,"保存自媒体账号失败");
        }
        return wmUser.getId();
    }
}
